package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.pub.MessageConstant;
import com.itheima.pub.RedisConstant;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference(mock = "com.itheima.service.SetmealServiceMock",timeout = 20000)
    SetmealService setmealService;

    @RequestMapping("findAllSetmeal")
    public Result findAllSetmeal(){
        List<Setmeal> list = new ArrayList<>();
        List<Setmeal>  redisDataList  = (List<Setmeal>) redisTemplate.opsForValue().get(RedisConstant.SETMEAL_INFO_RESOURCES);
        //System.out.println("redisData = " + redisDataList);
        if (redisDataList==null) {
            //从数据库查询
            //System.out.println("从数据库查询");
            try {
                list = setmealService.list();
                //保存到redis中
                redisTemplate.opsForValue().set(RedisConstant.SETMEAL_INFO_RESOURCES, list);

                return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, list);
            } catch (Exception e) {
                e.printStackTrace();
                return  new Result(true, e.getMessage());
            }

        }else {
            //从redis获取
            //System.out.println("从redis获取");
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, redisDataList);
        }

    }

    @RequestMapping("findSetMealDetail")
    public Result findSetMealDetail(@RequestParam("id") Integer id){
        Object list = redisTemplate.opsForValue().get(RedisConstant.SETMEAL_DETAIL_RESOURCES+id);
        if (list==null) {
            //从数据库查询
            System.out.println("从数据库查询");
            try {
                //保存到redis中
                Setmeal setmeal  = setmealService.findSetMealDetail(id);
                redisTemplate.opsForValue().set(RedisConstant.SETMEAL_DETAIL_RESOURCES+id, setmeal);

                return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
            } catch (Exception e) {
                e.printStackTrace();
                return  new Result(false, e.getMessage());
            }

        }else {
            //从redis获取
            //System.out.println("从redis获取");
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        }
    }

    @RequestMapping("findSetmealById")
    public Result findSetmealById(@RequestParam("id") Integer id){
        try {
            Setmeal setmeal = setmealService.findSetmealById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }
}
